// note 随记
// 对象关系：
//	   TradingAccount
//       /     \
//     Stock   Plan
//          \    |
//            TransactionRecord

package note

import (
	"trader/apps/note/handler"

	"github.com/gin-gonic/gin"
	"xorm.io/xorm"
)

func Init(router *gin.RouterGroup, engine *xorm.Engine) {

	accountHandler := handler.NewAccountHandler(engine)
	stockHandler := handler.NewStockHandler(engine)
	planHandler := handler.NewPlanHandler(engine)
	transHandler := handler.NewTransRecord(engine)

	router.GET("/trading_accounts", accountHandler.List)
	router.POST("/trading_accounts", accountHandler.Create)
	router.PUT("/trading_accounts/:trading_account_id", accountHandler.Update)
	router.DELETE("/trading_accounts/:trading_account_id", accountHandler.Delete)
	router.PUT("/trading_accounts/:trading_account_id/set-default", accountHandler.SetDefault)

	accountRouter := router.Group("/trading_accounts/:trading_account_id")
	accountRouter.Use(ValidUserTradingAccountMiddleware(engine))
	accountRouter.GET("/stocks", stockHandler.List)
	accountRouter.POST("/stocks", stockHandler.Create)
	accountRouter.GET("/stocks/:stock_id", stockHandler.Get)
	accountRouter.PUT("/stocks/:stock_id", stockHandler.Update)
	accountRouter.DELETE("/stocks/:stock_id", stockHandler.Delete)

	stockRouter := accountRouter.Group("/stocks/:stock_id")
	stockRouter.Use(ValidStockMiddleware(engine))
	stockRouter.GET("/plans", planHandler.List)
	stockRouter.POST("/plans", planHandler.Create)
	stockRouter.GET("/plans/:plan_id", planHandler.Get)
	stockRouter.PUT("/plans/:plan_id", planHandler.Update)
	stockRouter.DELETE("/plans/:plan_id", planHandler.Delete)
	stockRouter.PUT("/plans/:plan_id/set-status", planHandler.SetStatus)

	planRouter := stockRouter.Group("/plans/:plan_id")
	planRouter.Use(ValidPlanMiddleware(engine))
	planRouter.GET("/trans_records", transHandler.List)
	planRouter.POST("/trans_records", transHandler.Create)
	planRouter.GET("/trans_records/:trans_id", transHandler.Get)
	planRouter.PUT("/trans_records/:trans_id", transHandler.Update)
	planRouter.DELETE("/trans_records/:trans_id", transHandler.Delete)
}
